﻿using System;
using System.Collections.Generic;
using AnesSystem.Models;
using AnesSystem.Services;
using System.Linq;
using ExcelDll = Microsoft.Office.Interop.Excel;

namespace AnesSystem.Util
{
    public class CQFYExcel:ExcelBasic
    {

        protected override string[] GetQITiltles()//质控标题中文
        {
            string[] value = {
            "总手术数",
            "麻醉例数",
            "术后镇痛例数",
            "麻醉复苏室列数",
            "急诊手术例数",

            "ASA-I例数",
            "ASA-II例数",
            "ASA-III例数",
            "ASA-Ⅳ例数",
            "ASA-Ⅴ例数",
            "ASA-Ⅵ例数",

            "神经阻滞例数",
            "椎管内麻醉例数",
            "插管全麻例数",
            "复合麻醉例数",
            "静脉麻醉例数",
            "其它麻醉例数",

            "麻醉后手术取消例数",
            "非计划转入ICU例数",
            "非计划二次插管例数",
            "非计划二次插管例数",
            "麻醉后24小时内死亡例数",
            "麻醉后24小时内心脏骤停例数",
            "麻醉后新发昏迷例数",
            "心肺复苏例数",

            "穿破硬脊膜例数",
            "麻醉后头痛例数",
            "术中知晓例数",
            "麻醉期间严重过敏例数",
            "椎管内麻醉严重神经并发症例数",
            "全麻气管插管拔管后声音嘶哑例数",
            "氧饱和度重度降低例数",
            "术中严重低血压例数",

            "中心静脉穿刺例数",
            "颈内静脉穿刺例数",
            "锁骨上静脉穿刺数",
            "锁骨下静脉穿刺数",
            "股静脉穿刺例数",
            "中心静脉穿刺严重并发症例数",

            "桡动脉穿刺例数",
            "足背动脉穿刺例数",
            "有创动脉穿刺严重并发症例数",

            "术中输血不良反应例数",
            "用血合适例数",
            "用血不合适例数",
            "自体血回收例数",

            "手术室医疗干预例数",
            "非计划二次手术例数",
            "更改手术方式例数",
            "镇痛分娩转剖宫产例数",
            "急性失血>1600ml例数",
            "剖宫产手术时间大于3小时例数",
            "非手术范围器官损伤例数",

            "手术切口分类Ⅰ",
            "手术切口分类Ⅱ",
            "手术切口分类Ⅲ",
            "手术切口分类Ⅳ",
            "浅层组织手术例数",
            "深部组织手术例数",
            "器官手术例数",
            "腔隙内手术例数",

            "PACU转出延迟例数",
            "PACU入室低温例数",
            "术后躁动例数",
            "恶心呕吐例数",
            "低氧血症例数",
            "拔管后再插管例数",
            "苏醒延迟例数",
            "寒颤例数",
            "带管出室例数",
            "结束时使用催醒药物例数",

            "超声引导例数",
            "使用输注工作站例数",
            "血液加温治疗例数",
            "控制性降压治疗例数",
            "TEG例数",
            "BIS例数",
            "自体回输治疗例数",

            "麻醉效果1",
            "麻醉效果2",
            "麻醉效果3",
            "麻醉效果4",

            "转入病房例数",
            "转入ICU例数",
            "转入PACU例数", 
            };

            return value;
        }

        protected override string[] GetQIMethods()//质控标题对应字段
        {
            string[] methods = {
            "PatientLinkCount",
            "MaZuiCount",
            "ShuHouZhenTong",
            "PACUCount",
            "JiZhen",

            "ASA1",
            "ASA2",
            "ASA3",
            "ASA4",
            "ASA5",
            "ASA6",

            "ShenJingZuZhi",
            "ZhuiGuanNeiMaZauiCount",
            "ChaGuanQuanMaCount",
            "FuHeMaZuiCount",
            "JingMaiMaZuiConut",
            "QiTaMaZuiCount",

            "CancelShouShu",
            "ZhuanRuICU",
            "ErCiChaGuan",
            "NotPlan2MazuiFangShi",
            "Mazui24HourDead",
            "Mazui24HourStop",
            "HunMi",
            "XinFeiFuSu",

            "ChuanPoYingJiMo",
            "MazuiHouTouTong",
            "ShuZhongZhiXiao",
            "GuoMinYanZhong",
            "ShenJingBingFa",
            "QiGuanShengSi",
            "Spo2JiangDi",
            "ShuZhongDiXueYa",

            "ZhongXinJingMaiChuanCi",
            "JingNeiJingMaiChuanCi",
            "SuoGuShangJingMaiChuanCi",
            "SuoGuXiaJingMaiChuanCi",
            "GuJingMaiChuanCi",
            "JingMaiChuanCiBingFa",
            "NaoDongMaiChuanCi",
            "ZuBeiDongMaiChuanCi",
            "YouChuangDongMaiCCBingFa",

            "ShuZhongShuXueShuYeFY",
            "YongXueHeLiShu",
            "YongXueBuHeLiShu",
            "YongXueZiTiXueHuiShou",

            "ShouShuShiYiLiaoGanYu",
            "NotPlan2ShouShu",
            "GengGaiShouShuMethod",
            "ZhenTongFenZhuanPgc",
            "JiXingShiXue",
            "PouGongChanShouShu",
            "NotShouShuFanWeiQiGuanSunShang",

            "OperType1",
            "OperType2",
            "OperType3",
            "OperType4",
            "QianCengZuZhi",
            "ShenBuZuZhi",
            "QiGuan",
            "QiangXiNei",

            "PacuZhuanChuYanChi",
            "PacuRuShiTiWen",
            "PacuShuHouZaoDong",
            "PacuExinOutu",
            "PacuDiYangXueZheng",
            "PacuErCiBaGuan",
            "PacuSuXingYanChi",
            "PacuHanCan",
            "PacuDaiGuanChuShi",
            "PacuCuiXingYaoWu",

            "ChaoShengYinDao",
            "ShiYongShuZhuGongZuoZhan",
            "XueYeJiaWenZhiLiao",
            "KongZhiXingJiangYa",
            "Teg",
            "Bis",
            "AnesZiTiXueHuiShu",

            "QuanMaXiaoGuo1",
            "QuanMaXiaoGuo2",
            "QuanMaXiaoGuo3",
            "QuanMaXiaoGuo4",

            "BingFangCount",
            "ICUCount",
            "PACUCount"
            };
            return methods;
        }

        //质控excel填值
        protected override ExcelDll.Worksheet QIWorkbookProduction(string fileName, ExcelDll.Worksheet worksheet, List<QualityStatisticsModel> QIDataList)
        {
            ExcelDll.Range excelRange = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[2, 16]];

            //设置字体大小
            excelRange.Font.Size = 20;
            excelRange.Font.Bold = true;

            //设置字体在单元格内的对其方式
            excelRange.HorizontalAlignment = ExcelDll.XlHAlign.xlHAlignCenter;
            //自动调整列宽
            excelRange.EntireColumn.AutoFit();
            //文本自动换行
            excelRange.WrapText = true;

            //合并单元格
            excelRange.Merge(excelRange.MergeCells);
            worksheet.Cells[1, 1] = fileName;
            worksheet.Cells[3, 2] = "指标名称/日期";

            worksheet = merge(worksheet, "基本指标", 4, 1, 8,1);
            worksheet = merge(worksheet, "麻醉分级", 9, 1, 14, 1);
            worksheet = merge(worksheet, "麻醉种类", 15, 1, 20, 1);
            worksheet = merge(worksheet, "麻醉相关指标", 21, 1, 28, 1);
            worksheet = merge(worksheet, "麻醉并发症", 29, 1, 36, 1);
            worksheet = merge(worksheet, "中心静脉穿刺", 37, 1, 42, 1);
            worksheet = merge(worksheet, "动脉穿刺", 43, 1, 45, 1);
            worksheet = merge(worksheet, "输血相关指标", 46, 1, 49, 1);
            worksheet = merge(worksheet, "手术相关指标", 50, 1, 56, 1);
            worksheet = merge(worksheet, "感染相关指标", 57, 1, 64, 1);
            worksheet = merge(worksheet, "PACU并发症", 65, 1, 74, 1);
            worksheet = merge(worksheet, "麻醉相关操作", 75, 1, 81, 1);
            worksheet = merge(worksheet, "麻醉效果评定", 82, 1, 85, 1);
            worksheet = merge(worksheet, "患者去向", 86, 1, 88, 1);

            for (int i = 1; i < 13; i++)
            {
                worksheet.Cells[3, i + 2] = i + "月";
            }
            if (QIDataList.Count > 0)
            {

                getMonthsDates(QIDataList[0].Date.Year.ToString());

                for (int i = 0; i < GetQITiltles().Length; i++)
                {
                    worksheet.Cells[i + 4, 2] = GetQITiltles()[i];
                    for (int j = 0; j < months.Length; j++)
                    {
                        foreach (var item in QIDataList)
                        {
                            if (item.Date == months[j])
                            {
                                var propertyInfo = item.GetType().GetProperty(GetQIMethods()[i]);
                                if (propertyInfo != null)
                                {
                                    var value = propertyInfo.GetValue(item, null);
                                    worksheet.Cells[i + 4, j + 3] = value;
                                }
                            }
                        }
                    }
                }
            }

            return worksheet;
        }

    }
}
